﻿
/*
 * Copyright (C) Han Wei
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BSvr.PluginAPI;
using System.Reflection;
using System.IO;

namespace BSvr.PluginProxy
{
    /// <summary>
    /// Plugin在AppDomain中的代理
    /// </summary>
    public class Proxy : MarshalByRefObject,IPluginProxy
    {
        private static IPlugin _myPlugin = null;

        /// <summary>
        /// 加载插件
        /// </summary>
        /// <param name="pluginPath">插件目录地址</param>
        /// <exception cref="Exception">加载过程中的异常</exception>
        public static void LoadPlugin(string pluginno,string pluginPath)
        {
            UriBuilder ub = new UriBuilder(pluginPath);
            string dir = pluginPath;//Path.GetDirectoryName(ub.Uri.LocalPath);
            //if (!dir.EndsWith("\\"))
            //    dir += "\\";
            //dir += (pluginPath + "\\");
            
            try
            {
                string[] files = Directory.GetFiles(dir, "*.dll", SearchOption.TopDirectoryOnly);
                foreach (string item in files)
                {
                    Assembly am = Assembly.LoadFrom(item);
                    foreach (Type item1 in am.GetTypes())
                    {
                        Type tILog = item1.GetInterface("BSvr.PluginAPI.IPlugin");
                        if (tILog != null)
                        {
                            if (!item1.IsAbstract)
                            {
                                _myPlugin = System.Activator.CreateInstance(item1) as IPlugin;
                                if (_myPlugin == null)
                                {
                                    throw new Exception("没有实现BSvr.PluginAPI.IPlugin的接口对象");
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (_myPlugin != null)                                      // 单例模式，只需服务器初始化一次就可
                _myPlugin.Init(pluginno, pluginPath);
        }

        #region IPlugin 成员

        public string PluginNo
        {
            get
            {
                return _myPlugin.PluginNo;
            }
            set
            {
                _myPlugin.PluginNo = value;
            }
        }

        public string CurrentDirectory
        {
            get
            {
                return _myPlugin.CurrentDirectory;
            }
            set
            {
                _myPlugin.CurrentDirectory = value;
            }
        }

        public bool IsRunning
        {
            get { return _myPlugin.IsRunning; }
        }

        public BSvr.Model.OpRst<string> Excute(string cmd, List<string> cmdarg)
        {
            return _myPlugin.Excute(cmd, cmdarg);
        }

        public BSvr.Model.OpRst<string> Query(string query, List<string> queryarg)
        {
            return _myPlugin.Query(query, queryarg);
        }

        public BSvr.Model.OpRst<System.Data.DataTable> ExcuteConsole(string cmd, List<string> cmdarg)
        {
            return _myPlugin.ExcuteConsole(cmd, cmdarg);
        }

        public BSvr.Model.OpRst<string> Start()
        {
            return _myPlugin.Start();
        }

        public BSvr.Model.OpRst<string> End()
        {
            return _myPlugin.End();
        }

        public string Information
        {
            get { return _myPlugin.Information; }
        }

        public override object InitializeLifetimeService()          // 设置远程对象生存周期为整个程序 add by hanwei
        {
            return null;
        }

        #endregion
    }
}
